import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/find/acg_game_recommend_logic.dart';
import 'package:xvideos/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

//游戏推荐页面
class AcgGameRecommendPage extends StatefulWidget {
  final VideoModel? mediaModel;
  const AcgGameRecommendPage({super.key, this.mediaModel});

  @override
  State<AcgGameRecommendPage> createState() => _AcgGameRecommendPageState();
}

class _AcgGameRecommendPageState extends State<AcgGameRecommendPage> {
  late AcgGameRecommendLogic logic;
  late String tag;

  @override
  void initState() {
    super.initState();
    tag = (widget.mediaModel?.id ?? "") + DateTime.now().millisecondsSinceEpoch.toString();
    logic = Get.put(AcgGameRecommendLogic(mediaModel: widget.mediaModel), tag: tag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AcgGameRecommendLogic>(
      tag: tag,
      builder: (_) {
        if (_.dataList == null) return LoadingCenterWidget();
        if (_.dataList?.isEmpty == true)
          return CErrorWidget(
            errorMsg: '暂无数据',
            retryOnTap: () => _.loadData(),
          );
        return pullYsRefresh(
          onInit: (ctr) => _.refreshCtr = ctr,
          onLoading: (refreshcontroller) => _.loadMoreData(),
          enablePullDown: false,
          child: GridView.builder(
            physics: ClampingScrollPhysics(),
            padding: EdgeInsets.fromLTRB(16, 12, 16, 40),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 12,
              crossAxisSpacing: 5,
              childAspectRatio: 179 / 147,
            ),
            itemCount: _.dataList?.length ?? 0,
            itemBuilder: (context, index) {
              VideoModel? model = _.dataList?[index];
              return VideoSimpleCellWidget(
                videoModel: model,
                isFromHY: true,
                textLines: 1,
              );
            },
          ),
        );
      },
    );
  }
}
